<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\UploadedFile;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    
    public function json($data)
    {
        return response()->json([
            'code'=>0,
            'message'=>'ok',
            'data'=>$data
        ]);
    }
    
    public function originIcon($data){
        $d = [];
        foreach ($data as $l){
            $tmp = $l;
            if($tmp['icon']){
                $tmp['icon'] = Config::get('app.www_host').$tmp['icon'];
            }
            $d[] = $tmp;
        }
        return $d;
    }
    
    public function originImage($data){
        $d = [];
        foreach ($data as $l){
            $tmp = $l;
            $tmp['image'] = Config::get('app.www_host').$tmp['image'];
            $d[] = $tmp;
        }
        return $d;
    }
    
    public function error($message)
    {
        return response()->json([
            'code'=>1000,
            'message'=>$message,
        ]);
    }
    
    public function saveFile($name){
        $file = Input::file($name);
        if ($file) {
            $avatar = $this->saveToPublicUploadTmp($file);
            return $avatar;
        }
    }
    
    //**  保存到本地 upload_tmp 并返回图片地址
    public function saveToPublicUploadTmp(UploadedFile $file){
        $destinationPath = 'assets';
        $par = "/[\x80-\xff]/";
        $filename = $file->getClientOriginalName();
        $filetype  = pathinfo($filename, PATHINFO_EXTENSION);//获取后缀
        $filename = date("Ymdhis").".".$filetype; //构建新名称
        $file->move($destinationPath, $filename);
        $avatar = './assets/'.$filename; //取图片的时候用到
        $avatarNmae = 'assets/'.$filename;
        return $avatarNmae;
    }
}
